<?php
namespace App\Http\Controllers\V2\Payment;

use App\Http\Controllers\Controller;
use App\Libraries\Decorator\Image\CoverDecorator;
use App\Libraries\Decorator\Order\OrderDecoratorV2;
use App\Libraries\Decorator\Tool\IsEndDecorator;
use App\Libraries\Tools\ConstVar;
use App\Models\Order\Order;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;


class OrderController extends Controller{


    private $order;
    private $isEndDecorator;
    private $coverDecorator;
    private $orderDecoratorV2;

    public function __construct(Order $order, IsEndDecorator $isEndDecorator,
                                CoverDecorator $coverDecorator, OrderDecoratorV2 $orderDecoratorV2)
    {
        $this->order = $order;
        $this->isEndDecorator = $isEndDecorator;
        $this->coverDecorator = $coverDecorator;
        $this->orderDecoratorV2 = $orderDecoratorV2;
    }

    /**
     * 订单列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function orders(Request $request)
    {
        $valid_data = $request->all();
        if(!empty($request['status'])){
            $valid_data['status'] = \GuzzleHttp\json_decode($request['status'], true);
        }
        if(!empty($request['type'])){
            $valid_data['type'] = \GuzzleHttp\json_decode($request['type'], true);
        }
        $validator = Validator::make($valid_data, [
            'page' => 'required|integer',
            'size' => 'required|integer',
            'status.*' => 'in:0,1,2,3,4,5,6,7',
            'status' => 'array',
            'type.*' => 'in:1,2,3,4,5,6',
            'type' => 'array',
        ], [
            'page.required' => '10001',
            'page.integer' => '10002',
            'size.required' => '10001',
            'size.integer' => '10002',
            'status.*.in' => '10002',
            'status.array' => '10002',
            'type.*.in' => '10002',
            'type.array' => '10002',
        ]);
        if ($validator->fails()) {
            return $this->sendFailResponse(400, $validator->errors()->first());
        }
        $this->order->addDecorator($this->orderDecoratorV2);
        $this->order->addDecorator($this->isEndDecorator);
        $data = $this->order->ordersV2(
            $request['page'],
            $request['size'],
            Auth::id(),
            $valid_data['status']??null,
            $valid_data['type']??null
        );
        return $this->sendSuccessResponse($data);
    }
}